import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model 文件管理配置页面
 * @date: 2018-7-25


 */
import { getResponse, getCurrentOrganizationId } from 'utils/utils';
import { queryMapIdpValue } from 'cpaas-front/lib/services/api';
import { fetchDefaultStorage as _fetchDefaultStorage, fetchStorage as _fetchStorage, updateStorage as _updateStorage, deleteStorage as _deleteStorage, queryLdpTree } from "../services/storageService";
export default {
  namespace: 'storage',
  state: {
    storageDataList: [],
    // 文件配置数据
    tenantId: getCurrentOrganizationId(),
    // 当前用户所属租户id
    formKey: '',
    serverProviderList: [],
    // 权限控制父子值集
    prefixStrategyList: [],
    // 文件名前缀策略
    microsoftEndpointList: [],
    // 微软后缀
    radioTypeList: [] // 文件类型值集

  },
  effects: {
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_, _ref) {
      var put, call, _yield$getResponse, prefixStrategyList, microsoftEndpointList, radioTypeList, serverProviderList;

      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              put = _ref.put, call = _ref.call;
              _context.next = 3;
              return getResponse(call(queryMapIdpValue, {
                prefixStrategyList: 'CFLE.PREFIX_STRATEGY',
                microsoftEndpointList: 'CFLE.MICROSOFT.ENDPOINT_SUFFIX',
                radioTypeList: 'CFLE.SERVER_PROVIDER'
              }));

            case 3:
              _yield$getResponse = _context.sent;
              prefixStrategyList = _yield$getResponse.prefixStrategyList;
              microsoftEndpointList = _yield$getResponse.microsoftEndpointList;
              radioTypeList = _yield$getResponse.radioTypeList;
              _context.t0 = getResponse;
              _context.next = 10;
              return call(queryLdpTree, {
                'CFLE.SERVER_PROVIDER': 1,
                'CFLE.CAPACITY.ACCESS_CONTROL': 2
              });

            case 10:
              _context.t1 = _context.sent;
              serverProviderList = (0, _context.t0)(_context.t1);
              _context.next = 14;
              return put({
                type: 'updateState',
                payload: {
                  serverProviderList: serverProviderList,
                  prefixStrategyList: prefixStrategyList,
                  microsoftEndpointList: microsoftEndpointList,
                  radioTypeList: radioTypeList.filter(function (item) {
                    return item.tag !== 'UNUSE';
                  }) // 过滤未使用的

                }
              });

            case 14:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    fetchDefaultStorage: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDefaultStorage(_ref2, _ref3) {
      var payload, put, call, res, data;
      return _regeneratorRuntime.wrap(function fetchDefaultStorage$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              put = _ref3.put, call = _ref3.call;
              _context2.next = 4;
              return call(_fetchDefaultStorage, payload);

            case 4:
              res = _context2.sent;
              data = getResponse(res);

              if (!data) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  storageDataList: data && data.content
                }
              });

            case 9:
              return _context2.abrupt("return", data);

            case 10:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchDefaultStorage);
    }),
    fetchStorage: /*#__PURE__*/_regeneratorRuntime.mark(function fetchStorage(_ref4, _ref5) {
      var payload, put, call, res, data;
      return _regeneratorRuntime.wrap(function fetchStorage$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              put = _ref5.put, call = _ref5.call;
              _context3.next = 4;
              return call(_fetchStorage, payload);

            case 4:
              res = _context3.sent;
              data = getResponse(res);

              if (!data) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  storageDataList: data && data.content
                }
              });

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchStorage);
    }),
    updateStorage: /*#__PURE__*/_regeneratorRuntime.mark(function updateStorage(_ref6, _ref7) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateStorage$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(_updateStorage, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, updateStorage);
    }),
    deleteStorage: /*#__PURE__*/_regeneratorRuntime.mark(function deleteStorage(_ref8, _ref9) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteStorage$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call;
              _context5.next = 4;
              return call(_deleteStorage, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, deleteStorage);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref10) {
      var payload = _ref10.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};